package cn.com.iscs.sqlProject.filter;

import javax.servlet.*;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;

/**
 *
 * @author wwhhf
 * @since 2016年5月30日
 * @comment 跨域过滤器
 */
public class CrossOriginFilter implements Filter {


	@Override
	public void init(FilterConfig filterConfig) throws ServletException {

	}

	/**
	 *
	 * @author wwhhf
	 * @since 2016/5/30
	 * @comment 跨域的设置
	 */
	@Override
	public void doFilter(ServletRequest request, ServletResponse response,
	                     FilterChain chain) throws IOException, ServletException {
		HttpServletResponse httpResponse = (HttpServletResponse) response;
		// 表明它允许"http://xxx"发起跨域请求
		httpResponse.setHeader("Access-Control-Allow-Origin",
				"*");
		// 表明在xxx秒内，不需要再发送预检验请求，可以缓存该结果
		httpResponse.setHeader("Access-Control-Allow-Methods",
				"POST, GET, DELETE, PUT");
		// 表明它允许xxx的外域请求
		httpResponse.setHeader("Access-Control-Max-Age",
				"3628800");
		// 表明它允许跨域请求包含xxx头
		httpResponse.setHeader("Access-Control-Allow-Headers",
				"Content-Type");
		chain.doFilter(request, response);
	}

	@Override
	public void destroy() {

	}

}
